from base.api import BaseApi
from workflow import controllers as workflow_ctl


class WorkApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
    }

    def get(self, request, params):
        data = workflow_ctl.get_work(**params)
        return data


class ListWorkerApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
    }

    def get(self, request, params):
        data = workflow_ctl.get_workers(**params)
        return data


class WorkBaseInfoApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
    }

    def get(self, request, params):
        data = workflow_ctl.get_work_base_info(**params)
        return data


class ListWorkApi(BaseApi):

    need_params = {
        'keyword': ('关键词', 'optional str'),
        'typ': ('类型', 'optional str'),
        'mode': ('模式', 'optional str'),
        'page_num': ('页码', 'optional int'),
        'page_size': ('页容量', 'optional int'),
    }

    def get(self, request, params):
        data = workflow_ctl.get_works(**params)
        return data


class CreateWorkApi(BaseApi):

    need_params = {
        'name': ('工作名称', 'required str'),
        'host_id': ('主机ID', 'required int'),
        'typ': ('类型', 'required str'),
        'mode': ('模式', 'required str'),
        'line': ('线路', 'required int'),
        'script_id': ('脚本路径', 'required int'),
        'json_id': ('配置路径', 'optional int'),
        'injure_stop': ('受伤停止', 'required bool'),
        'soul_stop': ('丢魂停止', 'required bool'),
        'auto_restart': ('自动重启', 'required bool'),
        'auto_login': ('自动登录', 'required bool'),
        'skip_update': ('禁用更新', 'required bool'),
        'auto_change_line': ('自动换线', 'required bool'),
        'users': ('角色列表', 'required list'),
        'extra': ('额外参数', 'optional str'),
    }
    def post(self, request, params):
        workflow_ctl.create_work(**params)


class UpdateWorkApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
        'name': ('工作名称', 'required str'),
        'host_id': ('线路', 'required int'),
        'typ': ('类型', 'required str'),
        'mode': ('模式', 'required str'),
        'line': ('线路', 'required int'),
        'script_id': ('脚本路径', 'required int'),
        'json_id': ('配置路径', 'optional int'),
        'injure_stop': ('受伤停止', 'required bool'),
        'soul_stop': ('丢魂停止', 'required bool'),
        'auto_restart': ('自动重启', 'required bool'),
        'auto_login': ('自动登录', 'required bool'),
        'skip_update': ('禁用更新', 'required bool'),
        'auto_change_line': ('自动换线', 'required bool'),
        'extra': ('额外参数', 'optional str'),
    }
    def post(self, request, params):
        workflow_ctl.update_work(**params)


class DeleteWorkApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
    }
    def post(self, request, params):
        workflow_ctl.delete_work(**params)


class DeleteWorkerApi(BaseApi):

    need_params = {
        'work_id': ('工作ID', 'required int'),
        'user_id': ('角色ID', 'required int'),
    }
    def post(self, request, params):
        workflow_ctl.delete_worker(**params)


class RunWorkApi(BaseApi):

    need_params = {
        'obj_id': ('工作ID', 'required int'),
    }
    def post(self, request, params):
        workflow_ctl.run_work(**params)